package com.actions.bluetoothbox2.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.ibluz.manager.BluzManager;
import com.actions.ibluz.manager.BluzManagerData;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class AboutFragment extends Fragment {

    private final static String TAG = "AboutFragment";
    private BrowserActivity mActivity;
    private View mView;
    private BluzManager mBluzManager;
    private TextView app_version, tv_humidity, mac_addr, software_version;
    int cmd_type = Integer.decode("0x51");
    int cmd_id = Integer.decode("0x97");
    volatile boolean exit = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        mActivity = (BrowserActivity) getActivity();
        mView = inflater.inflate(R.layout.fragment_about, container, false);
        mActivity.tv_title.setText(getResources().getText(R.string.about));
        mActivity.title_img_right.setVisibility(View.GONE);
        mActivity.title_back.setVisibility(View.VISIBLE);
        mActivity.mGlobalInfoLayout.setVisibility(View.GONE);
        mActivity.getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        app_version = (TextView) mView.findViewById(R.id.app_version);
//        tv_temperature = (TextView) mView.findViewById(R.id.temperature);
        tv_humidity = (TextView) mView.findViewById(R.id.humidity);
        mac_addr = (TextView) mView.findViewById(R.id.mac_addr);
        software_version = (TextView) mView.findViewById(R.id.software_version);


        mBluzManager = mActivity.getBluzManager();
        mBluzManager.setOnCustomCommandListener(new BluzManagerData.OnCustomCommandListener() {
            @Override
            public void onReady(int i, int i1, int i2, byte[] bytes) {

                int d = bytes[0];
                tv_humidity.setText(d + "%");

                String macAddress = "";
                for (int j = 1; j < 7; j++) {
                    String hex = Integer.toHexString(bytes[j] & 0xFF);
                    if (hex.length() < 2) {
                        macAddress = macAddress + "0" + hex + ":";
                    } else {
                        macAddress = macAddress + hex + ":";
                    }
                }
                macAddress = macAddress.substring(0, macAddress.length() - 1);
                mac_addr.setText(macAddress);

                String str = "";
                StringBuffer tStringBuf = new StringBuffer();
                char[] tChars = new char[64];

                for (int j = 7; j < 71; j++) {
                    tChars[j - 7] = (char) bytes[j];
                }
                tStringBuf.append(tChars);
                str = tStringBuf.toString();
                software_version.setText(str);
            }
        });
//        Log.i("TAG", "=====sendCustomCommand");
        new MyThread().start();
        exit = true;
        return mView;
    }

    private class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                while (exit) {
                    mBluzManager.sendCustomCommand(cmd_type << 8 | cmd_id, 0, 0, null);
                    sleep(5000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onPause() {
        exit = false;
        super.onPause();
    }
}
